package com.carryking.smart.rpc.entity;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * Created by jincarry on 2018/1/10.
 */
public class ResponseFuture{

    private ResponseEntity responseEntity;

    private CountDownLatch latch;

    private int timeOut;

    public ResponseFuture(CountDownLatch latch,int timeOut) {
        this.latch = latch;
        this.timeOut = timeOut;
    }

    public void complete(ResponseEntity responseEntity) {
        this.responseEntity = responseEntity;
        latch.countDown();
        if (responseEntity.getException() != null) {
            responseEntity.getException().printStackTrace();
        }
    }

    public ResponseEntity get() throws InterruptedException, ExecutionException {
        latch.await(timeOut, TimeUnit.MILLISECONDS);
        return responseEntity;
    }

}
